//
//  NSDate+Addition.m
//  JieJieKan
//
//  Created by Geeven Duan on 2017/7/31.
//  Copyright © 2017年 innext. All rights reserved.
//

#import "NSDate+Addition.h"

@implementation SYJDateFormatter

+ (instancetype)shared {
    
    static  SYJDateFormatter *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SYJDateFormatter alloc]init];
    });
    return instance;
}

@end

@implementation NSDate (Addition)

+ (NSString *)timeStamp:(NSInteger)timeStamp timeFormat:(NSString *)formate{
    [SYJDateFormatter shared].dateFormat = formate;
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
//    NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:formate];
    return [[SYJDateFormatter shared] stringFromDate:confromTimesp];
}

+ (NSString *)gd_dateStringWithDelta:(NSTimeInterval)delta {
    
    [SYJDateFormatter shared].dateFormat = @"yyyy-MM-dd HH:mm:ss";
    [SYJDateFormatter shared].locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    NSDate * date = [NSDate dateWithTimeIntervalSinceNow:delta];
    return [[SYJDateFormatter shared] stringFromDate:date];
}

+ (NSDate *)gd_sinceDateOne:(NSString *)create_at {
    
    [SYJDateFormatter shared].dateFormat = @"yyyy-MM-dd HH:mm:ss";
    [SYJDateFormatter shared].locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    return [[SYJDateFormatter shared] dateFromString:create_at];
}


+ (NSDate *)gd_sinceDate:(NSString *)create_at {
    
    [SYJDateFormatter shared].dateFormat = @"EEE MMM dd HH:mm:ss zzz yyyy";
    [SYJDateFormatter shared].locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    return [[SYJDateFormatter shared] dateFromString:create_at];
}


- (NSString *)gd_dateDescription {
    
    if ([[NSCalendar currentCalendar] isDateInToday:self]) {
        NSTimeInterval delta = [self timeIntervalSinceNow];
        delta = fabs(delta);
        NSInteger deltaTime = (NSInteger)delta;
        if (delta < 60) {
            return @"刚刚";
        }else if (delta < 60 * 60){
            return [NSString stringWithFormat:@"%zd分钟前",deltaTime / 60];
        }else {
            return [NSString stringWithFormat:@"%zd小时前",deltaTime / 60 / 60];
        }
    }
    
    //    NSString * formate = @" HH:mm";
    if ([[NSCalendar currentCalendar] isDateInYesterday:self]) {
        //        formate = [NSString stringWithFormat:@"昨天%@",formate];
        return @"昨天";
    }else {
        //        formate = [NSString stringWithFormat:@"MM-dd %@",formate];
        NSInteger year = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:self];
        NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
        if (year != thisYear) {
            //            formate = [NSString stringWithFormat:@"yyyy-%@",formate];
            return [NSString stringWithFormat:@"%zd年前",thisYear - year];
        }else {
            NSInteger month = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:self];
            NSInteger thisMonth = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]];
            if (month != thisMonth) {
                return [NSString stringWithFormat:@"%zd月前",thisMonth - month];
            }else {
                NSInteger day = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:self];
                NSInteger thisDay = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]];
                return [NSString stringWithFormat:@"%zd天前",thisDay - day];
            }
        }
    }
    //    [SYJDateFormatter shared].dateFormat = formate;
    //    [SYJDateFormatter shared].locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    //    return [[SYJDateFormatter shared] stringFromDate:self];
    
}

@end
